

package com.itheima62.chat;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ListView;

import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechRecognizer;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SpeechUtility;
import com.iflytek.cloud.SynthesizerListener;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;

public class MainActivity extends Activity
{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 将“12345678”替换成您申请的APPID，申请地址：http://open.voicecloud.cn
		SpeechUtility.createUtility(this, SpeechConstant.APPID + "=55a07e68");
		setContentView(R.layout.activity_main);
		ListView lv_message;
		
	}
	public void speak(View v){
		InitListener mInitListener = new InitListener() {
			
			@Override
			public void onInit(int arg0) {
				// TODO Auto-generated method stub
				
			}
		};
		//1.创建SpeechRecognizer对象，第二个参数：本地听写时传InitListener 
		RecognizerDialog  iatDialog = new RecognizerDialog(this,mInitListener ); 
		//2.设置听写参数，同上节 
		//3.设置回调接口 
		iatDialog.setListener(new RecognizerDialogListener() {
			
			@Override
			public void onResult(RecognizerResult arg0, boolean arg1) {
				// TODO Auto-generated method stub
				Log.d("Result:",
						arg0.getResultString());
			}
			
			@Override
			public void onError(SpeechError arg0) {
				// TODO Auto-generated method stub
				
			}
		}); 
		//4.开始听写 
		iatDialog.show(); 
	}
	
	public void speak1(View v) {
		//1.创建SpeechSynthesizer对象, 第二个参数：本地合成时传InitListener 
		SpeechSynthesizer mTts= SpeechSynthesizer.createSynthesizer(this, null); 
		//2.合成参数设置，详见《科大讯飞MSC API手册(Android)》SpeechSynthesizer 类
		//设置发音人（更多在线发音人，用户可参见 附录12.2 
		mTts.setParameter(SpeechConstant.VOICE_NAME, "vixm");   
		mTts.setParameter(SpeechConstant.SPEED, "50");//设置语速 
		mTts.setParameter(SpeechConstant.VOLUME, "80");//设置音量，范围0~100 
		mTts.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
		//设置合成音频保存位置（可自定义保存位置），保存在“./sdcard/iflytek.pcm” 
		//保存在SD卡需要在AndroidManifest.xml添加写SD卡权限 
		//仅支持保存为pcm格式，如果不需要保存合成音频，注释该行代码 
		mTts.setParameter(SpeechConstant.TTS_AUDIO_PATH, "./sdcard/iflytek.pcm"); 
		//3.开始合成 
		mTts.startSpeaking("中午吃什么?红烧肉如何", mSynListener); 
	}
	
	//合成监听器 
	private SynthesizerListener mSynListener = new SynthesizerListener(){

		@Override
		public void onBufferProgress(int arg0, int arg1, int arg2, String arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onCompleted(SpeechError arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onEvent(int arg0, int arg1, int arg2, Bundle arg3) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSpeakBegin() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSpeakPaused() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSpeakProgress(int arg0, int arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onSpeakResumed() {
			// TODO Auto-generated method stub
			
		} 
	
	};
	

	public void listen(View v) {
		// 1.创建SpeechRecognizer对象，第二个参数：本地听写时传InitListener

		SpeechRecognizer mIat = SpeechRecognizer.createRecognizer(this, null);
		// 2.设置听写参数，详见《科大讯飞MSC API手册(Android)》SpeechConstant类
		mIat.setParameter(SpeechConstant.DOMAIN, "iat");
		mIat.setParameter(SpeechConstant.LANGUAGE, "zh_cn");
		mIat.setParameter(SpeechConstant.ACCENT, "mandarin");
		mIat.startListening(mRecoListener); //监听回调结果
	}

	// 3.开始听写 mIat.startListening(mRecoListener);
	// 听写监听器
	private RecognizerListener	mRecoListener	= new RecognizerListener() {
													// 听写结果回调接口(返回Json格式结果，用户可参见附录12.1)；
													// 一般情况下会通过onResults接口多次返回结果，完整的识别内容是多次结果的累加；
													// 关于解析Json的代码可参见MscDemo中JsonParser类；
													// isLast等于true时会话结束。
													public void onResult(
															RecognizerResult results,
															boolean isLast) {
														Log.d("Result:",
																results.getResultString());
													}

													// 开始录音
													public void onBeginOfSpeech() {
													}

													// 音量值0~30
													public void onVolumeChanged(
															int volume) {
													}

													// 结束录音
													public void onEndOfSpeech() {
													}

													// 扩展用接口
													public void onEvent(
															int eventType,
															int arg1, int arg2,
															Bundle obj) {
													}

													@Override
													public void onError(
															SpeechError arg0) {

													}

												};

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
